package com.jack.database_exp.component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
@SuppressWarnings("all")
public class LoginHandlerInterceptor implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception{
        String path = request.getServletPath();
        HttpSession session = request.getSession();
        //如果没有user这一属性,说明没有登录成功过
        if(path.equals("/customerBuy")){
            Object loginCustomer = session.getAttribute("loginCustomer");
            if(loginCustomer==null){
                request.setAttribute("msg","无权访问");
                request.getRequestDispatcher("/customerLogin").forward(request,response);
                return false;
            }else{
                return true;
            }
        }else{
            Object user = session.getAttribute("loginUser");
            if(user==null){
                //无法通过那么我们需要的操作就是跳转到登陆界面
                request.setAttribute("msg","无权访问");
                request.getRequestDispatcher("/index").forward(request,response);
                return false;//拦截
            }else{
                return true;
            }
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}